﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1.RouteEvnet
{
    class TimeRouteEventArgs : RoutedEventArgs
    {
        public TimeRouteEventArgs(RoutedEvent routedEvent,object source):base(routedEvent,source)
        {

        }

        public DateTime ClickTime { get; set; }
    }

    class TimeButton : Button
    {
        //声明和注册路由事件\
        public static readonly RoutedEvent ReportTimeRoutedEvent =
            EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<TimeRouteEventArgs>), typeof(TimeButton));
        //CLR事件包装
        public event RoutedEventHandler ReportTime
        {
            add { this.AddHandler(ReportTimeRoutedEvent, value); }
            remove { this.RemoveHandler(ReportTimeRoutedEvent, value); }
        }
        //激发路由事件,借用Click事件的激发方法

        protected override void OnClick()
        {
            base.OnClick();//保证Button原有功能正常使用，Click事件被激发

            TimeRouteEventArgs args = new TimeRouteEventArgs(ReportTimeRoutedEvent, this);
            args.ClickTime = DateTime.Now;
            this.RaiseEvent(args);//UIElement及其派生类            
        }

    }
}
